﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using WindowsFormsApplication1;
using DLL.Entities;
using DLL.Entities.Config;
using System.Xml;
using System.Windows.Forms;

namespace DLL
{
    class Serializer
    {

        COMconfig COMobj = new COMconfig();
        MISconfig MISobj = new MISconfig();
        RADconfig RADobj = new RADconfig();

        Missile Missileobj = new Missile();
        Target Targetobj = new Target();

        #region Config_Reader
        XmlReader reader = null;

        public COMconfig ReadCOMconfig()
        {
            try
            {
                reader = new XmlTextReader(@"..\..\..\DLL\XML\COMconfig");
                XmlSerializer serializer = new XmlSerializer(typeof(COMconfig));
                COMobj = (COMconfig)serializer.Deserialize(reader);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                reader.Close();
            }

            return COMobj;
        }


        public MISconfig ReadMISconfig()
        {
            try
            {
                reader = new XmlTextReader(@"..\..\..\DLL\XML\MISconfig");
                XmlSerializer serializer = new XmlSerializer(typeof(MISconfig));
                MISobj = (MISconfig)serializer.Deserialize(reader);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                reader.Close();
            }

            return MISobj;
        }

        public RADconfig ReadRADconfig()
        {
            try
            {
                reader = new XmlTextReader(@"..\..\..\DLL\XML\RADconfig");
                XmlSerializer serializer = new XmlSerializer(typeof(RADconfig));
                RADobj = (RADconfig)serializer.Deserialize(reader);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                reader.Close();
            }

            return RADobj;
        }
        #endregion


        #region Entities_Reader

        public Missile ReadMissile()
        {
            try
            {
                reader = new XmlTextReader(@"..\..\..\DLL\XML\Missile");
                XmlSerializer serializer = new XmlSerializer(typeof(COMconfig));
                Missileobj = (Missile)serializer.Deserialize(reader);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                reader.Close();
            }

            return Missileobj;

        }

        public Target ReadTarget()
        {
            try
            {
                reader = new XmlTextReader(@"..\..\..\DLL\XML\Target");
                XmlSerializer serializer = new XmlSerializer(typeof(Target));
                Targetobj = (Target)serializer.Deserialize(reader);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                reader.Close();
            }

            return Targetobj;
        }

        #endregion

        StreamWriter Writer = null;

        public void WriteMissile(int Remaining, string dir)
        {
            Missileobj.MissileCount = Remaining;

            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Missile));
                Writer = new StreamWriter(dir);
                serializer.Serialize(Writer, Missileobj);
                Writer.Close();
            }
            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                Writer.Close();
            }


        }


        public void WriteTarget(Target target, string dir)
        {
            Targetobj.TargetAltitude = target.TargetAltitude;
            Targetobj.TargetDistance = target.TargetDistance;
            Targetobj.TargetSize = target.TargetSize;
            Targetobj.TargetSpeed = target.TargetSpeed;
            Targetobj.TargetThermo = target.TargetThermo;
            Targetobj.targetType = target.targetType;

            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Missile));
                Writer = new StreamWriter(dir);
                serializer.Serialize(Writer, Missileobj);
                Writer.Close();
            }
            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                Writer.Close();
            }


        }




        #region Joe Code
        /* private void timer1_Tick(object sender, EventArgs e)
        {
            //Code in the DLL that accepts three input parameters: the Generic object type holding the data to save, the filename to save, and the serializer object
public static bool WriteXML(object savedObj, string fileToSave, XmlSerializer serializer)
        {
            try
            {
                TextWriter textWriter = new StreamWriter(fileToSave);
                serializer.Serialize(textWriter, savedObj);
                textWriter.Close();
                return true;
            }
            catch (Exception ex)
            {
                Utility.LogFile(ex.ToString());
                return false;
            }
        }

//Code in RAD, COM, or MIS app to call the WriteXML DLL static method
  XmlSerializer serializer = new XmlSerializer(typeof(Missile));  //You can replace Bogey with any type you are trying to serialize
  XMLHandler.WriteXML(Missile, "Bogey_Serialzed.xml", serializer); //You can replace "Bogey_Serialzed.xml" with the file name needed

        }

    public static bool ReadXML(object recvedObj out, string fileToRead, XmlSerializer serializer)
    {
           try
            {
                TextWriter textWriter = new StreamWriter(fileToSave);
                serializer.Serialize(textWriter, savedObj);
                textWriter.Close();
                return true;
            }
            catch (Exception ex)
            {
                Utility.LogFile(ex.ToString());
                return false;
            }


    } 

        */
        #endregion

    }


}
